import { PipeTransform, Pipe, Injectable } from '@angular/core'
import { TranslateService } from '@ngx-translate/core'

@Pipe({
  name: 'i18nParams'
})
@Injectable({
  providedIn: 'root',
})
export class I18NParams implements PipeTransform {
  constructor(
    private translate: TranslateService,
  ) {}
  transform(str: string, params?: object) {
    if (!(params instanceof Object)) {
      console.log('params translate 出错, 传入的 params 不是正确对象')
      return
    }
    for (const i in params) {
      params[i] = this.translate.instant(params[i])
    }
    return this.translate.instant(str, params)
  }
}
